package com.dchh.android.suverynew.views;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.view.MyPopupWindow;
import com.dhcc.android.base.utils.Constants;

import java.util.Map;

/**
 * 地块操作
 *
 * @author zhoudan
 * @date 2016年1月7日
 */
public class DKOperateWindow {

    private MyPopupWindow pw;
    private Activity activity;
    private Map<String, String> data;
    private OnOperateClickListener onOperate;

    public DKOperateWindow(Activity activity, Map<String, String> data) {
        this.activity = activity;
        this.data = data;
    }


    public void show(View parent, boolean cfflag, boolean scflag, OnOperateClickListener onOperate) {
        this.onOperate = onOperate;
        if (pw == null) {
            View view = LayoutInflater.from(activity).inflate(R.layout.util_dk_operate_window, null);
            LinearLayout dkinfo = (LinearLayout) view.findViewById(R.id.dk_info);
            TextView dkid = (TextView) view.findViewById(R.id.dk_id);
            TextView dkcun = (TextView) view.findViewById(R.id.dk_cun);
            TextView dkyfbh = (TextView) view.findViewById(R.id.dk_yfbh);
            TextView dktblx = (TextView) view.findViewById(R.id.dk_tblx);
            LinearLayout dkopbtn = (LinearLayout) view.findViewById(R.id.dk_op_btn);
            LinearLayout dkcf = (LinearLayout) view.findViewById(R.id.dk_cf);
            LinearLayout dksc = (LinearLayout) view.findViewById(R.id.dk_sc);

            dkid.setText(data.get("YFDKBH"));
            dkcun.setText(data.get("CUNMC"));
            dkyfbh.setText(data.get("YFBHU"));
            dktblx.setText(data.get("TBLXMC"));

            dkinfo.setOnClickListener(click);
            dkcf.setOnClickListener(click);
            dksc.setOnClickListener(click);

            if (cfflag) {
                dkcf.setVisibility(View.VISIBLE);
            } else {
                dkcf.setVisibility(View.GONE);
            }
            if (scflag) {
                dksc.setVisibility(View.VISIBLE);
            } else {
                dksc.setVisibility(View.GONE);
            }

            pw = new MyPopupWindow(view, Constants.screenWidth, LayoutParams.WRAP_CONTENT, false);
        }

        pw.showAtLocation(parent, Gravity.BOTTOM, 0, 0);// 显示在最中央的位置
    }

    public void dismiss() {
        if (pw != null) {
            pw.dismiss();
        }
    }

    OnClickListener click = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.dk_info) {
                if (onOperate != null) {
                    onOperate.OnReport(data);
                }
            } else if (v.getId() == R.id.dk_sc) {
                if (onOperate != null) {
                    onOperate.OnShanChu(data);
                }
            } else if (v.getId() == R.id.dk_cf) {
                if (onOperate != null) {
                    onOperate.OnChaifen(data);
                }

            }
            pw.dismiss();
        }
    };

    public interface OnOperateClickListener {
        public void OnReport(Map<String, String> data);

        public void OnShanChu(Map<String, String> data);

        public void OnChaifen(Map<String, String> data);
    }
}
